package cpr.view;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

import cpr.controller.ClienteCprRMI;

import bean.vo.ProveedorVO;;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class EliminarProveedor extends javax.swing.JFrame {
	private JButton btnEliminar;
	private AbstractAction eliminarAction;
	private JLabel lblCliente;
	private EntityComboBox<ProveedorVO> cmbProveedores;
	
	public EliminarProveedor() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Eliminar Proveedor");
			this.setPreferredSize(new java.awt.Dimension(700, 150));
			{
				lblCliente = new JLabel();
				getContentPane().add(lblCliente);
				lblCliente.setText("Seleccionar Proveedor: ");
				lblCliente.setBounds(6, 22, 138, 14);
			}
			{
				List<ProveedorVO> vc = ClienteCprRMI.getInstance().getIRemota().getProveedores();
				if (vc == null){
					JOptionPane.showMessageDialog(null, "No se encuentran proveedores a eliminar.", "No existen Proveedores", JOptionPane.WARNING_MESSAGE);	
				}
				
				cmbProveedores = new EntityComboBox<ProveedorVO>();
				cmbProveedores.setItems(vc);
				getContentPane().add(cmbProveedores);
				cmbProveedores.setBounds(156, 19, 500, 21);				
			}
			{
				btnEliminar = new JButton();
				getContentPane().add(btnEliminar);
				btnEliminar.setText("Eliminar");
				btnEliminar.setBounds(304, 66, 92, 33);
				btnEliminar.setFont(new java.awt.Font("Tahoma",1,11));
				btnEliminar.setAction(getEliminarAction());
			}
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private AbstractAction getEliminarAction() {
		if(eliminarAction == null) {
			eliminarAction = new AbstractAction("Eliminar", null) {
				public void actionPerformed(ActionEvent evt) {
					try{
						ProveedorVO p = cmbProveedores.getSelectedItem();
						ClienteCprRMI.getInstance().getIRemota().bajaProveedor(p);
						cmbProveedores.removeItemAt(cmbProveedores.getSelectedIndex());
						JOptionPane.showMessageDialog(null, "Proveedor eliminado con exito.", "Resultado", JOptionPane.INFORMATION_MESSAGE);
					}catch  (RemoteException e){
						String message = e.getMessage();
						int index = message.indexOf("java.rmi.RemoteException: ");
						message = message.substring(index + 26, message.length());
						JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
					} catch (Exception e){
						JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			};
		}
		return eliminarAction;
	}
	
}
